package com.aluka.nirvana.framework.core.api;

import cn.hutool.core.io.IoUtil;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 配置信息获取
 * @author gongli
 * @version 1.0
 * @since 11:00 2020/9/24
 */
@RestController
public class PropertiesApi {

    @GetMapping("/p")
    public void getProperties(HttpServletResponse response) throws IOException {
        response.setContentType(MediaType.TEXT_HTML_VALUE);
        Resource resource = new ClassPathResource("properties.html");
        IoUtil.write(response.getOutputStream(), true, IoUtil.readBytes(resource.getInputStream()));
    }

    private void appendSecurity(StringBuffer sb) {
        appendElement(sb, "nirvana.security.open-resources", "开发的资源，多个用逗号分隔，支持通配符[*!%$&]", "string", "/doc.html");
        appendElement(sb, "nirvana.security.login-url", "默认登录地址", "string", "/login");
        appendElement(sb, "nirvana.security.logout-url", "默认注销地址", "string", "/logout");
        appendElement(sb, "nirvana.security.api-valid", "是否开启接口权限过滤", "boolean", "false");
    }

    private void appendSwagger(StringBuffer sb) {
        appendElement(sb, "nirvana.swagger.base-package", "扫描的包路径", "string", "cn");
        appendElement(sb, "nirvana.swagger.title", "接口文档标题", "string", "未知");
        appendElement(sb, "nirvana.swagger.description", "接口文档描述", "string", "未知");
        appendElement(sb, "nirvana.swagger.version", "接口文档版本", "string", "V1.0");
    }

    private void appendDatasource(StringBuffer sb) {
        appendElement(sb, "nirvana.database.jpaEnabled", "是否开启使用Jpa配置", "boolean", "true");
        appendElement(sb, "nirvana.database.mybatisEnabled", "是否开启使用Mybatis配置", "boolean", "true");
        appendElement(sb, "nirvana.database.isAnnotation", "是否使用注解切换数据源 @TargetDataSource(\"hq\")", "boolean", "false");
        appendElement(sb, "nirvana.database.isEncrypt", "是否加密，如果需要加密则需要自定义实现类EncryptAware", "boolean", "false");
        appendElement(sb, "nirvana.database.datasource", "数据源具体配置，多个数据源情况下只需要数据源名称不一致即可", "array", "无");
        appendElement(sb, "nirvana.database.datasource.XX.isPrimary", "XX数据源:是否是主数据源，", "boolean", "false");
        appendElement(sb, "nirvana.database.XX.driverClassName", "XX数据源:驱动类型", "string", "无");
        appendElement(sb, "nirvana.database.XX.url", "XX数据源:数据源连接串", "string", "无");
        appendElement(sb, "nirvana.database.XX.username", "XX数据源:账号", "string", "无");
        appendElement(sb, "nirvana.database.XX.password", "XX数据源:密码", "string", "无");
        appendElement(sb, "nirvana.database.XX.mapperLocations", "XX数据源:Mybatis的Mapper.xml资源路径，如 classpath:mapper/*", "string", "无");
        appendElement(sb, "nirvana.database.XX.mapperPackages", "XX数据源:Mybatis的Mapper.java文件扫描包路径，多个用逗号隔开", "string", "无");
        appendElement(sb, "nirvana.database.XX.entityPackages", "XX数据源:Jpa的ORM实体扫描包路径，多个用逗号隔开", "string", "无");
        appendElement(sb, "nirvana.database.XX.repositoryPackages", "XX数据源:Jpa的Repository.java文件扫描包路径，多个用逗号隔开", "string", "无");
    }

    private void appendCache(StringBuffer sb) {
        appendElement(sb, "nirvana.caches", "缓存池配置", "array", "无");
        appendElement(sb, "nirvana.caches.XX.cacheType.", "缓存类型", "string", "无");
        appendElement(sb, "nirvana.caches.XX.enabled.", "配置说明", "string", "无");
    }

    private void appendHead(StringBuffer sb){
        appendElement(sb, "配置项", "配置说明", "数据类型", "默认值");
    }

    private void appendElement(StringBuffer sb, String name, String desc, String type, String defaultVal){
        sb.append("<tr>\n");
        sb.append(" <td>");
        sb.append(name);
        sb.append(" </td>\n");
        sb.append(" <td>");
        sb.append(desc);
        sb.append(" </td>\n");
        sb.append(" <td>");
        sb.append(type);
        sb.append(" </td>\n");
        sb.append(" <td>");
        sb.append(defaultVal);
        sb.append(" </td>\n");
        sb.append("</tr>\n");
    }

}
